
package org.arl.modem.codec;
import java.util.*;

public class RevisedRobustSoliton {

    protected int k;
    protected int R;
    
    protected float c;
 
    protected float[] rho;
    protected float[] tau;
    protected float[] mu;
    protected float beta;
    protected float delta;
 /*
 * Description: Generates RS distribution and returns the closest value that can produce the probability specified in rando
Input values: K- Number of source messages (k)
              c – The constant c for the RS distribution
              rando – The random probability that generates a degree. The random probability is generated using Kent Maps.
              delta   -  The parameter δ in RS distribution
Returns: The closest integer value that can generate the probability specified in the input parameters using RS distribution.

 */
   public int randomnumgen(int K, float c,float rando,float delta) {
        this.k=K;
        this.delta=delta;
        this.c=c;
        int index=0;
        R=(int) (c*Math.log((double)k/delta)*Math.sqrt((double)k));
        rho=new float[k+1];
        tau=new float[k+1];
        mu=new float[k+1];
        Random randGen=null;
        randGen=new Random();
        
        //System.out.println(" R is " + R);
        for (index=1;index<=k;index++) {
            if (index==1) {
                rho[index]=(float) 1/k;
            } else {
                rho[index]=(float) 1/((index)*((index)-1));
            }
            //System.out.println(rho[index]);
        }
       // System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        for (index=1;index<=k;index++) {
            if (index<(k/(R-1))) {
                tau[index]=(float) R/((index)*k);
            } else if (index==k/R) {
                tau[index]=(float) (R*Math.log((double)R/delta)/k);
            } else {
                tau[index]=0;
            }
            //System.out.println(tau[index]);
        }
        beta=0;
        for (index=1;index<=k;index++) {
            beta+=rho[index]+tau[index];
        }
       // System.out.println("beta >>>>>>>>>>>>>>>>>>>>>>>>" + beta);
        for (index=1;index<=k;index++) {
            mu[index]=(float) (rho[index]+tau[index])/beta;
            //System.out.println(mu[index]);
        }
        float temp1=0;
        float temp2=0;
        float disc;
       
        
        
            temp1=0;
            temp2=0;
            disc=0;
       // System.out.println(rando);
        disc=(float) (Math.sqrt((double) Math.pow(k*rando*beta+R,(double) 2)-4*(R-k)*(k*beta*rando)));
        if (disc >0) {
            temp1= (float)((float) (k*beta*rando+R)+disc);
            temp2= (float)((float) (k*beta*rando+R)-disc);
            if (temp1>0) {
                temp1=Math.round((float) (temp1/(2*(k*beta*rando))));
            } 
            if (temp2>0) {
                temp2=Math.round((float) (temp2/(2*(k*beta*rando))));
            }
        }
        
        
        //System.out.println("temp 1 " + temp1 + " temp2 " + temp2);
  
        /*for (index=0;index<k;index++) {
            if (rando==mu[k/R]]) {
                
            } else if (mu[index] rando) {
                
            }
        }*/
        return Math.round(temp1);
       
    }
}


